import Ember from 'ember';
import _ from 'lodash/lodash';

/*
 *  数据加载服务，一般用于首次连接，或者需要同步数据的时候
 */
export default Ember.Service.extend({
  store: Ember.inject.service('store'),
  statusService: Ember.inject.service("current-status"),

  init() {
    this._super(...arguments);
    var _self = this;
    //预加载字典数据
    Ember.run.later(function(){
      _self.syncDictData();
      _self.syncConfigData();
    },1);
  },
  //加载首次连接的数据
  loadBaseData: function () {
    console.log("loadBaseData in");
    //初始化测试数据

  },
  getMobileMenu: function () {
    // return Ember.ArrayProxy.extend({
    //   //按照序号排序
    //   arrangedContent: Ember.computed.sort('content', 'props'),
    //   props: ['order:asc']
    // }).create({
    //   content: this.get("store").filter("localstorage/privilege", function (privilege){
    //               console.log("privilege mobileMenuName in filter:" + privilege.get("mobileMenuName") + " with id:" + privilege.get("id"));
    //               //只查有移动端菜单的
    //               return !Ember.isEmpty(privilege.get("mobileMenuName"));
    //             })
    // });
    var _self = this;
    var menus = new Ember.A();
    return new Ember.RSVP.Promise(function (resolve, reject) {
      _self.get("store").query("localstorage/privilege",{filter:{level:1}}).then(function(privileges){
        privileges.forEach(function(privilege){
          console.log("privilege menu in",privilege);
          if(privilege.get("isMobile")===1){
            menus.pushObject(privilege);
          }
        });
        Ember.RSVP.all(menus).then(function (menuList) {
          menuList = _.sortBy(menuList,function(item){
            return item.get("order");
          });
          console.log("menuList get", menuList);
          resolve(menuList);
        });
      });
    });
  },
  //同步远程的字典数据
  syncDictData: function () {
    console.log("do dict query");
    var _self = this;

    //然后同步添加
    this.get("store").query("dicttype",{}).then(function (dicttypes) {
      let dictArr = new Ember.A();
      dicttypes.forEach(function(dicttype){
      dicttype.set("typegroupCode",dicttype.get("typegroup.typegroupcode"));
        dictArr.pushObject(dicttype);
      });
      _self.set("dicttypes",dictArr);
    });

  },
  //同步远程的权限数据
  syncPrivilegeData: function () {
    console.log("do Privilege query");
    var _self = this;
    //然后同步添加
    this.get("store").query("privilege",{}).then(function (privileges) {
      var arr = new Ember.A();
      privileges.forEach(function(privilege){
        arr.pushObject(privilege);
      });
      _self.set("privileges",arr);
    });
  },
  //同步远程的配置数据
  syncConfigData: function () {
    console.log("do config query");
    var _self = this;
    // this.set("sysconfig",Ember.Object.create({sysTime:1480497048}));
    this.get("store").findAll("sysconfig").then(function (sysconfig) {
      _self.set("sysconfig",sysconfig.get("firstObject"));
    });
  },
  //查询某个字典数据
  findDict: function(typecode){
    var dict = this.get("dicttypes").findBy("typecode",typecode);
    return dict;
  },
  //查询某个字典数组
  findDictList: function(typegroupCode){
    return this.get("dicttypes").filterBy("typegroupCode",typegroupCode);
  }
});
